<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-form-item label="变更金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input-group v-decorator="['changesAmount', validatorRules.changesAmount]" :compact="true"> <!--style="display: inline-block; vertical-align: middle"-->
              <a-select v-decorator="['changesType', validatorRules.changesType]" style="width: 100px" :trigger-change="true">
                <a-select-option :value="1">增加</a-select-option>
                <a-select-option :value="2">减少</a-select-option>
              </a-select>
              <a-input-number v-decorator="['changesAmount', validatorRules.changesAmount]" placeholder="变更金额" :style="{width: 'calc(100% - 100px)'}" />
            </a-input-group>
          </a-form-item>

          <a-form-item v-is-group label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-company-select-tag placeholder="请选择所属公司" v-decorator="[ 'companyId', validatorRules.companyId]"
                                  :trigger-change="true" />
          </a-form-item>

          <a-form-item label="变更描述说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-textarea v-decorator="['description', validatorRules.description]" rows="4"
                        placeholder="请输入变更描述说明"></a-textarea>
          </a-form-item>

        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'
import JUpload from '@/components/jeecg/JUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { updateCustomerBalance } from '@/api/customer'
import JCompanySelectTag from '@comp/jeecg/JCompanySelectTag'
import { IS_GROUP } from '@/store/mutation-types'

export default {
  name: 'UpdateCustomerBalanceModal',
  components: {
    JDate,
    JUpload,
    JCompanySelectTag,
    JDictSelectTag
  },
  data () {
    return {
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      width: 700,
      disableSubmit: false,
      confirmLoading: false,
      visible: false,
      title: '',
      model: {},
      validatorRules: {
        changesType: {
          rules: [
            { required: true, message: '请选择变更类型!' }
          ]
        },
        changesAmount: {
          rules: [
            { required: true, message: '请输入变更金额!' },
            { pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '请输入零以上的数字' }
          ]
        },
        description: {
          rules: [
            { required: true, message: '请输入变更说明描述!' }
          ]
        },
      }
    }
  },
  methods: {
    show (record) {
      this.confirmLoading = false
      this.title = '客户余额变更'
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.model.changesType = 1

      let isGroup = sessionStorage.getItem(IS_GROUP)
      if (isGroup == 1) {
        let companyName = {
          rules: [{
            required: true, message: '请选择所属公司!'
          }]
        }
        this.validatorRules.companyId = companyName
      }

      let fieldval = pick(this.model, 'changesType')
      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          // let formData = Object.assign(this.model, values)
          let formData = values
          formData.customerId = this.model.id
          updateCustomerBalance(formData).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
              this.close()
            } else {
              this.$message.warning(res.message)
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        }

      })
    },
    handleCancel () {
      this.close()
    },
    close () {
      this.$emit('close')
      this.visible = false
    }
  }
}
</script>

<style scoped>
</style>